#include "transferfile.h"

//********************************************************************************
//接收指令函数
//********************************************************************************
task_t *recvCmd(int peerfd)
{
	task_t *ptask = (task_t *)calloc(1, sizeof(task_t)); // calloc同时也进行了初始化操作

	ptask->peerfd = peerfd;

	//先接收指令类型;
	recvCircle(peerfd, (char *)&ptask->type, 4);

	printf("指令类型为：%d\n", ptask->type);

	//再接收指令参数
	int cmdlength = 0;
	memset(&ptask->data,0,1024);
	recvCircle(peerfd, (char *)&cmdlength, 4);
	recvCircle(peerfd, ptask->data, cmdlength);
	printf(">>>>datasize:%ld\n",strlen(ptask->data));
	return ptask;
}

//********************************************************************************
//接收信息函数，接收length长度信息到buff
//********************************************************************************
int recvCircle(int fd, char *buff, int length)
{
	char *pbuf = buff;
	int left = length;
	int ret = 0;
	while (left > 0)
	{
		ret = recv(fd, pbuf, left, 0);
		if (0 == ret)
		{
			close(fd);
			break;
		}
		else if (ret == -1 && errno == EINTR)
		{
			continue;
		}
		else if (ret == -1)
		{
			perror("recv");
			break;
		}
		else
		{ // > 0
			pbuf += ret;
			left -= ret;
		}
	}
	return length - left;
}

void InitFileque(pfileQue fileque)
{
	fileque->front = NULL;
	fileque->rear = NULL;
	fileque->length = 0;
	pthread_mutex_init(&fileque->mutex, NULL);
	pthread_cond_init(&fileque->cond, NULL);
}

void DestroyFileque(pfileQue fileque)
{
	while(fileque->front != NULL)
	{
		pfile_t Temp = fileque->front;
		fileque->front = fileque->front->next;
		free(Temp);
	}
	pthread_mutex_destroy(&fileque->mutex);
	pthread_cond_destroy(&fileque->cond);
}

void FilequeEnque(pfileQue fileque, pfile_t pfileNode)
{
	if (fileque->front == NULL)
	{
		fileque->front = fileque->rear = pfileNode;
	}
	else
	{
		fileque->rear->next = pfileNode;
		fileque->rear = pfileNode;
	}
	fileque->length++;
}

void FilequeRecoverque(pfileQue fileque, pfile_t pfileNode)
{
	strcpy(pfileNode->filename,"");
	pfileNode->fileRecvSize = 0;
	pfileNode->fileSys = 0;
	pfileNode->fileSize = 0;
	strcpy(pfileNode->md5,"");
	pfileNode->md5Sys = 0;
	pfileNode->next = NULL;
	pfileNode->endRecv = 0;
}

void FilequeDeque(pfileQue fileque, pfile_t pfileNode)
{
	//判断是否头节点
	if (fileque->front == pfileNode)
	{
		fileque->front = fileque->front->next;
	}
	else
	{
		file_t dumyNode;
		dumyNode.next = fileque->front;
		pfile_t head = &dumyNode;
		while (head->next != pfileNode)
		{
			head = head->next;
		}
		head->next = head->next->next;

		//判断是否尾结点
		if (pfileNode == fileque->rear)
		{
			fileque->rear = head;
		}
	}
	free(pfileNode);
}

pfile_t findNode(int peerfd, pfileQue fileque)
{
	file_t *head = fileque->front;
	while (head != NULL)
	{
		if (peerfd == head->peerfd)
		{
			return head;
		}
		head = head->next;
	}
	return NULL;
}

void AddfilequeNode(pfileQue fileque,int peerfd)
{
	printf("In AddfilequeNode\n");
	pthread_mutex_lock(&fileque->mutex);
	printf("AddfilequeNode加锁完成\n");
	pfile_t Node = findNode(peerfd,fileque);
	printf("findNode\n");
	if(Node != NULL)
	{
		printf("peerfd %d 存在，初始化 Node->peerfd\n",peerfd);
		memset(Node,0,sizeof(file_t));
		Node->peerfd = peerfd;
	}
	else
	{
		printf("创建文件结点\n");
		pfile_t fileNode = (pfile_t)calloc(1,sizeof(file_t));
		fileNode->peerfd = peerfd;
		FilequeEnque(fileque,fileNode);
	}
	pthread_mutex_unlock(&fileque->mutex);
	printf("结束\n");
}